/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hjt2;

/**
 *
 * @author julio
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author julio
 */
public class MainDriver {
    
    StackVector<Double> data = new StackVector<Double>();
    double oper1,oper2;
    
    JFrame frame;
    
    JPanel panelMain;
    
    JPanel panelMenu;
    JPanel panelCalcu;
        JButton buttonSuma;
        JButton buttonResta;
        JButton buttonMult;
        JButton buttonDiv;
        
    JPanel panelInput;
        JButton buttonEnter;
        JTextField tfEnter;
        JButton buttonExit;
        
    JPanel panelDisplay;
        JLabel labelOper1;
        JLabel labelOper2;
        JLabel labelOperacion;
       
    public MainDriver(){
        
        oper1 = oper2 = 0;
        
        FlowLayout fL = new FlowLayout();
	BorderLayout bL = new BorderLayout();
        BorderLayout bL2 = new BorderLayout();
        
        panelMain = new JPanel();
        panelMain.setLayout(bL);
        
        panelMenu = new JPanel();
        panelMenu.setLayout(bL2);
        
        panelCalcu = new JPanel();
            buttonSuma = new JButton("+");buttonSuma.setName("suma");
            buttonResta = new JButton("-");buttonResta.setName("resta");
            buttonMult = new JButton("*");buttonMult.setName("mult");
            buttonDiv = new JButton("/");buttonDiv.setName("div");
            
        panelCalcu.setLayout(fL);
        panelCalcu.add(buttonSuma);
        panelCalcu.add(buttonResta);
        panelCalcu.add(buttonMult);
        panelCalcu.add(buttonDiv);
            
        panelInput = new JPanel();
            buttonEnter = new JButton("Enter");buttonEnter.setName("enter");
            tfEnter = new JTextField(5);
            buttonExit = new JButton("Exit");buttonExit.setName("exit");
            
        panelInput.setLayout(fL);
        panelInput.add(buttonEnter);panelInput.add(tfEnter);panelInput.add(buttonExit);
        
        panelMenu.add(panelCalcu,"North");
        panelMenu.add(panelInput,"South");
        
        panelDisplay = new JPanel();
            labelOper1 = new JLabel(""); 
            labelOper2 = new JLabel("");
            labelOperacion = new JLabel("");
            
        panelDisplay.add(labelOper1);panelDisplay.add(labelOper2);panelDisplay.add(labelOperacion);
        
        panelMain.add(panelMenu,"West");
        panelMain.add(panelDisplay,"East");
        
        frame = new JFrame("Calculadora(Stack)");
	frame.setContentPane(panelMain);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(500, 500, 400, 75);
	frame.setResizable(false);
	frame.setVisible(true);
        
        enableCalcu(false);
        
        ButtonEngine bEng = new ButtonEngine();
        buttonSuma.addActionListener(bEng);
        buttonResta.addActionListener(bEng);
        buttonMult.addActionListener(bEng);
        buttonDiv.addActionListener(bEng);
        buttonEnter.addActionListener(bEng);
        buttonExit.addActionListener(bEng);
    }
    
    public double suma(){
        popData();
        double suma = oper1 + oper2;
        return suma;
    }
    
    public double div(){
        popData();
        double div = oper1 / oper2;
        return div;
    }
    
    public double resta(){
        popData();
        double resta = oper1 - oper2;
        return resta;
    }
    
    public double mult(){
        popData();
        double mult = oper1 * oper2;
        return mult;
    }
    
    public void popData(){
        oper2 = data.pop();
        oper1 = data.pop();
    }
    
    public void pushData(Double oper){
        data.push(oper);
    }
    
    public void setLabelOper1(double oper){
        labelOper1.setText(String.valueOf(oper));
        labelOper2.setText("");
        enableCalcu(false);
    }
    
     public void setLabelOper2(double oper){
        labelOper2.setText(String.valueOf(oper));
        enableCalcu(true);
    }
    
    public void enter(){
        if(data.empty()){
                oper1 = Double.valueOf(tfEnter.getText().toString());
                pushData(oper1);
                setLabelOper1(oper1);
            }else if(data.size()<2){
                oper2 = Double.valueOf(tfEnter.getText().toString());
                pushData(oper2);
                setLabelOper2(oper2);
            }
    } 
     
    public void enableCalcu(boolean status){
        buttonSuma.setEnabled(status);
        buttonResta.setEnabled(status);
        buttonMult.setEnabled(status);
        buttonDiv.setEnabled(status);
    }
    
    public void logicButtons(JButton button){
        String name = button.getName();
        
        if(name.compareTo("suma")==0){
            oper1 = suma();
            pushData(oper1);
            setLabelOper1(oper1);
        }else if(name.compareTo("resta")==0){
            oper1 = resta();
            pushData(oper1);
            setLabelOper1(oper1);
        }else if(name.compareTo("mult")==0){
            oper1 = mult();
            pushData(oper1);
            setLabelOper1(oper1);
        }else if(name.compareTo("div")==0){
            oper1 = div();
            pushData(oper1);
            setLabelOper1(oper1);
        }else if(name.compareTo("enter")==0){
            enter();
            tfEnter.setText("");
        }else if(name.compareTo("exit")==0){
            System.exit(0);
        }
    }
    
    public static void main(String[] Args){
        new MainDriver();
    }
    
    public class ButtonEngine implements ActionListener{

	public void actionPerformed(ActionEvent e) {

            JButton clickedButton = (JButton) e.getSource();
            logicButtons(clickedButton);	
	}
		
    }
}
